public class 面试题判定是否互为字符重排 {
    //https://leetcode.cn/problems/check-permutation-lcci/description/
    //方法一, 使用位运算
    //防止aa bb这样子的字符串出现, 所以要加上sum来判断和是否相同
    public boolean CheckPermutation(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        int xor = 0;
        int sum1 = 0;
        int sum2 = 0;
        for(int i = 0;i < s1.length();i++){
            int a = s1.charAt(i);
            int b = s2.charAt(i);
            xor ^= a;
            xor ^= b;
            sum1 += a;
            sum2 += b;
        }
        return xor == 0 && sum1 == sum2;
    }
}
